package ldh.im.base.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import ldh.common.PageResult;
import ldh.im.base.dao.ImUserDao;
import ldh.im.base.pojo.ImUser;
import ldh.im.base.pojo.where.ImUserWhere;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: ldh
 * @date: 2024-03-21 10:36:19
*/
@Service("imUserService")
public class ImUserService {

	@Resource
	private ImUserDao imUserDao;
	
	@Transactional
	public ImUser insert(ImUser imUser) {
		int n = imUserDao.insert(imUser);
		if (n != 1) {
			throw new RuntimeException("ImUser insert error!");
		}
		return imUser;
	}
	
	@Transactional
	public ImUser insertSelective(ImUser imUser) {
		int n = imUserDao.insert(imUser);
		if (n != 1) {
			throw new RuntimeException("ImUser insertSelective error!");
		}
		return imUser;
	}

	@Transactional
	public ImUser updateById(ImUser imUser) {
		int  n = imUserDao.updateById(imUser);
		if (n != 1) {
			throw new RuntimeException("ImUser update error!");
		}
        return imUser;
     }

	@Transactional
	public ImUser updateNotNullById(ImUser imUser) {
		int  n = imUserDao.updateNotNullById(imUser);
		if (n != 1) {
			throw new RuntimeException("ImUser update error!");
		}
        return imUser;
     }


    @Transactional
    public void deleteById(Long id) {
		int n = imUserDao.deleteById(id);
		if (n != 1) {
			throw new RuntimeException("ImUser delete error!");
		}
    }

	@Transactional(readOnly = true)
	public ImUser getById(Long id) {
		ImUser imUser = imUserDao.getById(id);
		return imUser;
    }

	@Transactional(readOnly = true)
	public ImUser getByIdAndTenantCode(Long id, String tenantCode) {
		ImUser imUser = imUserDao.getByIdAndTenantCode(id, tenantCode);
		return imUser;
	}

	@Transactional(readOnly = true)
	public ImUser getByCodeAndTenantCode(String code, String tenantCode) {
		return imUserDao.getByCodeAndTenantCode(code, tenantCode);
	}
	
	@Transactional(readOnly = true)
	public PageResult<ImUser> findByImUserWhere(ImUserWhere imUserWhere) {
		long total = imUserDao.findTotalByImUserWhere(imUserWhere);
		List<ImUser> imageList = new ArrayList<ImUser>();
		if (total > 0) {
			imageList  = imUserDao.findByImUserWhere(imUserWhere);
		}
		
		return new PageResult<ImUser>(imUserWhere, total, imageList);
	}

	@Transactional(readOnly = true)
	public List<ImUser> getAllChangeImUsers(ImUser imUser, Date preRequestDate) {
		ImUserWhere imUserWhere = new ImUserWhere();
		imUserWhere.setOwnerId(imUser.getId());
		imUserWhere.setStartUpdateTime(preRequestDate);
		List<ImUser> userList = imUserDao.getAllChangeUsers(imUserWhere);
		return userList;
	}
}
